package com.roger.uia.helper.manager;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.support.test.InstrumentationRegistry;
import android.util.Log;
import java.util.List;

import static com.roger.uia.helper.UiBase.mDevice;

public class Package {
    private static final String TAG = "Test" + Package.class.getSimpleName();
    private PackageManager pacManager = InstrumentationRegistry.getTargetContext().getPackageManager();

    public static Package pac() { return new Package(); }

    public boolean isAppInstalled(String pacName) {
        List<PackageInfo> packageList = pacManager.getInstalledPackages(0);
        if (packageList != null) {
            for (int i = 0; i < packageList.size(); i++) {
                String pName = packageList.get(i).packageName;
                if (pName.equals(pacName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public String getAppLabel(String pacName){
        CharSequence Label = null;
        if(getAppInfo(pacName)!=null) Label = pacManager.getApplicationLabel(getAppInfo(pacName));
        return String.valueOf(Label);
    }


    public String getAppInstallPath(String pacName){
        try {
            return pacManager.getApplicationInfo(pacName, 0).sourceDir;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String[] getAppPerms(String pacName){
        try {
            return pacManager.getPackageInfo(pacName,PackageManager.GET_PERMISSIONS).requestedPermissions;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void grantAllPerms(String pacName){
        try {
            String[] permissions = getAppPerms(pacName) ;
            if(permissions == null) return;
            for (String p : permissions) {
                int code = pacManager.checkPermission(p, pacName);
                if (code != PackageManager.PERMISSION_GRANTED){
                    mDevice.executeShellCommand("pm grant " + pacName + " " + p);
                }
            }
        } catch (Throwable e) {
            Log.d(TAG,pacName + ": grant perms fail" );
            e.printStackTrace();
        }
    }

    private ApplicationInfo getAppInfo(String pacName){
        List<PackageInfo> packageList = pacManager.getInstalledPackages(0);
        if (packageList != null) {
            for (int i = 0; i < packageList.size(); i++) {
                String pName = packageList.get(i).packageName;
                if(pName.equals(pacName)){
                    return packageList.get(i).applicationInfo;
                }
            }
        }
        return null;
    }

    public void getOtherAppRes(String pacName){
        try {
            Resources rs = pacManager.getResourcesForApplication(pacName);
            int ivId = rs.getIdentifier("ic_launcher", "mipmap", "com.yf.test");
            int tvId = rs.getIdentifier("app_name", "string", pacName);
            Log.d(TAG, rs.getString(tvId));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

}
